package com.zhihuichongcheng.application.ui.openclass.activity;

import android.app.DownloadManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.openclass.SubCategory;
import com.zhihuichongcheng.application.ui.cricle.adapter.HealthyAdapter;
import com.zhihuichongcheng.application.ui.openclass.adapter.OpenClassSubCategoryAdapter;
import com.zhihuichongcheng.application.ui.openclass.fragment.OpenClassStudyFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 3water on 2016/10/29.
 */

public class KnowledgeWorldActivity extends BaseActivity implements View.OnClickListener {
    private ViewPager mVpHealthy;
    private HealthyAdapter mAdapter = null;
    private List<Fragment> mFragments = new ArrayList<>();
    private String mTitle;
    private String mALlCategoryId;
    private ArrayList<SubCategory> list = null;
    private OpenClassSubCategoryAdapter mOpenClassSubCategoryAdapter = null;
    private RecyclerView mRcvCategory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_knowledge_world);
        initView();
        initData();
    }

    private void initView() {
        mRcvCategory = (RecyclerView) findViewById(R.id.rcv_category);
        mVpHealthy = (ViewPager) findViewById(R.id.vp_knowledge);
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("title")) {
            mTitle = intent.getStringExtra("title");

        }
        setTitle(mTitle);
        if (intent != null && intent.hasExtra("list")) {
            list = intent.getParcelableArrayListExtra("list");
        }
        if (intent != null && intent.hasExtra("allCategoryId")) {
            mALlCategoryId = intent.getStringExtra("allCategoryId");
        }
        setImRight(R.drawable.ic_download_list, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(DownloadManager.ACTION_VIEW_DOWNLOADS);
            }
        });
        SubCategory category = new SubCategory();
        category.setCategoryName("全部");
        category.setOpenClassCategoryId(mALlCategoryId);
        list.add(0, category);
        GridLayoutManager manager = new GridLayoutManager(mContext, list.size());
        mRcvCategory.setLayoutManager(manager);
        mOpenClassSubCategoryAdapter = new OpenClassSubCategoryAdapter(mContext);
        mOpenClassSubCategoryAdapter.setData(list);
        mOpenClassSubCategoryAdapter.setPosition(0);
        mRcvCategory.setAdapter(mOpenClassSubCategoryAdapter);
        mOpenClassSubCategoryAdapter.setOnItemOnclick(new OnItemOnclick<SubCategory>() {
            @Override
            public void onClick(SubCategory subCategory, int position) {
                mOpenClassSubCategoryAdapter.setPosition(position);
                mVpHealthy.setCurrentItem(position, false);
            }
        });
        setTitle(mTitle);
        clear();
        for (int i = 0; i < list.size(); i++) {
            OpenClassStudyFragment fragment = new OpenClassStudyFragment();
            Bundle bundle = new Bundle();
            bundle.putString("id", list.get(i).getOpenClassCategoryId());
            fragment.setArguments(bundle);
            mFragments.add(fragment);
        }
        mAdapter = new HealthyAdapter(getSupportFragmentManager(), mFragments);
        mVpHealthy.setOffscreenPageLimit(mFragments.size());
        mVpHealthy.setAdapter(mAdapter);
        mVpHealthy.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mOpenClassSubCategoryAdapter.setPosition(position);
                mOpenClassSubCategoryAdapter.notifyDataSetChanged();

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void clear() {
    }

    @Override
    public void onClick(View v) {
    }

}
